

Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Data.SqlClient
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Portals
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Common
Imports DotNetNuke.Security

''' -----------------------------------------------------------------------------
''' Namespace:  RTP.RSSFeed
''' Project:    RTP.RSSFeed
''' Class:      RssWN
''' -----------------------------------------------------------------------------
''' <summary>
''' Outputs the RSS feed 
''' </summary>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
'''		[RWJS]	03/07/2008	created
''' </history>
''' -----------------------------------------------------------------------------
Namespace RTP.RSSFeed

    Partial Public Class RSSGen
        Inherits System.Web.UI.Page

#Region " Private Members "
        Private m_moduleID As Integer = Null.NullInteger
        Private m_tabID As Integer = Null.NullInteger
        Private bl_ViewPerms As Boolean = False
        Private RSSSettings As RSSSettings
#End Region


#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim blSecurity As Boolean
            Dim _portalSettings As PortalSettings = CType(HttpContext.Current.Items("PortalSettings"), PortalSettings)

            ReadQueryString()
            If m_moduleID = Null.NullInteger Then
                Response.Redirect(NavigateURL(), True)
            End If

            Dim objModuleController As New ModuleController

            RSSSettings = New RSSSettings(m_moduleID)
            blSecurity = RSSSettings.rsssecurity


            If blSecurity Then
                If m_tabID = Null.NullInteger Then
                    Response.Redirect(NavigateURL(), True)
                End If

                Dim ModuleConfiguration As ModuleInfo = objModuleController.GetModule(m_moduleID, m_tabID, False)

                If DotNetNuke.Security.Permissions.ModulePermissionController.CanViewModule(ModuleConfiguration) Then
                    bl_ViewPerms = True
                Else
                    bl_ViewPerms = False
                End If
            Else
                bl_ViewPerms = True
            End If



            Response.ContentType = "text/xml"
            Response.ContentEncoding = Encoding.UTF8

            Dim sw As StringWriter = New StringWriter
            Dim writer As XmlTextWriter = New XmlTextWriter(sw)

            writer.WriteStartElement("rss")
            writer.WriteAttributeString("version", "2.0")
            writer.WriteAttributeString("xmlns:atom", "http://www.w3.org/2005/Atom")
            writer.WriteAttributeString("xmlns:wfw", "http://wellformedweb.org/CommentAPI/")
            writer.WriteAttributeString("xmlns:slash", "http://purl.org/rss/1.0/modules/slash/")
            writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/")
            writer.WriteAttributeString("xmlns:trackback", "http://madskills.com/public/xml/rss/module/trackback/")

            writer.WriteStartElement("channel")

            writer.WriteElementString("title", _portalSettings.PortalName & " - " & RSSSettings.rssfeedroottitle)

            writer.WriteStartElement("atom:link")
            writer.WriteAttributeString("href", Request.Url.AbsoluteUri)
            writer.WriteAttributeString("rel", "self")
            writer.WriteAttributeString("type", "application/rss+xml")
            writer.WriteEndElement()

            Dim txtLink As String
            If (_portalSettings.PortalAlias.HTTPAlias.IndexOf("http://") = -1) Then
                txtLink = "http://" & _portalSettings.PortalAlias.HTTPAlias
            Else
                txtLink = _portalSettings.PortalAlias.HTTPAlias
            End If
            writer.WriteElementString("link", AddUTMTags(txtLink))

            writer.WriteElementString("description", RSSSettings.rssfeedrootdescription)
            writer.WriteElementString("ttl", "60")

            If bl_ViewPerms Then

                Dim RSSRows As New RSSDB
                Dim SqlDr As SqlDataReader = RSSRows.GetRows(RSSSettings.rssfeedsqlquery)
                Dim tmpText As String = ""
                While SqlDr.Read()
                    writer.WriteStartElement("item")
                    If RSSSettings.rssfeedtitle Then
                        tmpText = CType(SqlDr("Title"), String)
                        tmpText = Replace(tmpText, "&rsquo;", "'")
                        tmpText = Replace(tmpText, "&lsquo;", "'")
                        writer.WriteElementString("title", tmpText)
                    End If
                    Dim txtImageDescription As String = ""
                    If RSSSettings.rssimageurl Then
                        If Not (CType(SqlDr("ImageURL"), String)) = Nothing Then
                            txtImageDescription = "<br /><a href=""" & CType(SqlDr("Link"), String) & """><img src=""" & CType(SqlDr("ImageURL"), String) & """"
                            If Not (CType(SqlDr("ImageHeight"), String)) = Nothing Then
                                txtImageDescription = txtImageDescription & " height=""" & CType(SqlDr("ImageHeight"), String) & """"
                            End If
                            If Not (CType(SqlDr("ImageWidth"), String)) = Nothing Then
                                txtImageDescription = txtImageDescription & " width=""" & CType(SqlDr("ImageWidth"), String) & """"
                            End If
                            txtImageDescription = txtImageDescription & " /></a>"
                        End If
                    End If

                    If RSSSettings.rssfeeddescription Then
                        tmpText = CType(SqlDr("Description"), String)
                        tmpText = RemoveAttribute(tmpText, "onclick=""", """")
                        tmpText = RemoveAttribute(tmpText, "onload=""", """")
                        tmpText = RemoveAttribute(tmpText, "<embed", "/>")
                        tmpText = RemoveAttribute(tmpText, "<o:p>", "</o:p>")
                        tmpText = RemoveAttribute(tmpText, "<script", "</script>")
                        tmpText = RemoveAttribute(tmpText, "background-image:", ";")
                        tmpText = AddHref(tmpText)
                        writer.WriteElementString("description", tmpText & txtImageDescription)
                    End If
                    If RSSSettings.rssfeedlink Then
                        tmpText = CType(SqlDr("Link"), String)
                        writer.WriteElementString("link", AddUTMTags(tmpText))
                    End If
                    If RSSSettings.rssfeedguid Then
                        tmpText = CType(SqlDr("Link"), String)
                        writer.WriteElementString("guid", AddUTMTags(tmpText))
                    End If
                    If RSSSettings.rssfeedpubdate Then
                        If RSSSettings.rssutcconvert Then
                            writer.WriteElementString("pubDate", CType(SqlDr("PubDate"), DateTime).ToUniversalTime().ToString("r"))
                        Else
                            writer.WriteElementString("pubDate", CType(SqlDr("PubDate"), DateTime).ToString("r"))
                        End If
                    End If
                    If RSSSettings.rssfeedcreator Then
                        writer.WriteElementString("dc:creator", CType(SqlDr("Creator"), String))
                    End If
                    If RSSSettings.rssfeedcategory Then
                        writer.WriteElementString("category", CType(SqlDr("Category"), String))
                    End If
                    writer.WriteEndElement()

                End While

                SqlDr.Close()
            Else
                writer.WriteStartElement("item")
                If RSSSettings.rssfeedtitle Then
                    writer.WriteElementString("title", RSSSettings.rssnoviewperms)
                End If

                If RSSSettings.rssfeeddescription Then
                    writer.WriteElementString("description", RSSSettings.rssnoviewperms)
                End If
                If RSSSettings.rssfeedlink Then
                    writer.WriteElementString("link", _portalSettings.PortalAlias.HTTPAlias)
                End If
                If RSSSettings.rssfeedpubdate Then
                    writer.WriteElementString("pubDate", Date.Now.ToUniversalTime().ToString("r"))
                End If
                writer.WriteEndElement()
            End If

            writer.WriteEndElement()
            writer.WriteEndElement()

            Response.Write(sw.ToString)

        End Sub

#End Region

#Region " Private Methods "

        Private Sub ReadQueryString()

            If (Request("ModuleID") <> "") Then
                If (IsNumeric(Request("ModuleID"))) Then
                    m_moduleID = Convert.ToInt32(Request("ModuleID"))
                End If
            End If

            If (Request("TabID") <> "") Then
                If (IsNumeric(Request("TabID"))) Then
                    m_tabID = Convert.ToInt32(Request("TabID"))
                End If
            End If

        End Sub

        Private Function AddUTMTags(ByVal inLink As String) As String
            Dim outLink As String = inLink
            Dim addChar As String = "?"
            If InStr(inLink, "?") > 0 Then
                addChar = "&"
            End If

            If RSSSettings.rssutmsource <> "" Then
                outLink = outLink & addChar & "utm_source=" & RSSSettings.rssutmsource
                addChar = "&"
            End If

            If RSSSettings.rssutmmedium <> "" Then
                outLink = outLink & addChar & "utm_medium=" & RSSSettings.rssutmmedium
                addChar = "&"
            End If

            Return outLink
        End Function

        Private Function RemoveAttribute(ByVal inString As String, ByVal startAttr As String, ByVal endAttr As String) As String
            Do While InStr(inString, startAttr) > 0
                Dim startPos As Integer = InStr(inString, startAttr)
                Dim inStringFront As String = Left(inString, startPos - 1)
                Dim inStringMiddle As String = Mid(inString, startPos + Len(startAttr))
                Dim endPos As Integer = InStr(inStringMiddle, endAttr)
                Dim inStringBack As String = Mid(inStringMiddle, endPos + Len(endAttr))
                inString = inStringFront + inStringBack
            Loop
            Return inString
        End Function

        Private Function AddHref(ByVal Content As String) As String
            Dim domainurl As String = HttpContext.Current.Request.ServerVariables("HTTP_HOST")
            If domainurl.IndexOf("/") > 0 Then
                domainurl = domainurl.Substring(0, domainurl.IndexOf("/"))
            End If

            Dim txtContent As String = Content
            If domainurl <> "" Then
                txtContent = Replace(txtContent, "src=""/", "src=""http://" & domainurl & "/")
                txtContent = Replace(txtContent, "href=""/", "href=""http://" & domainurl & "/")
            End If
            Return txtContent
        End Function


#End Region

    End Class

End Namespace